<?php 

Class adModel extends appModel
{
	/**
	 * 获取版块内容详情
	 * @param $id
	 * @return 
	 */
	public function getImg($id)
	{
		$sql = "SELECT * FROM ".$this->table('block_content')." WHERE id = '".$id."'";
		$result = $this->getRow($sql);
		if($result)
		{
			$arr3 = hy_dig_filter($result['title'], 3);
			$result['en_title'] = $arr3[1];
			$result['title'] = $arr3[0];
			
			$arr = hy_dig_filter($result['description'], 3);
			$result['en_description'] = $arr[1];
			$result['description'] = $arr[0];
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	
	/**
	 * 删除版块信息
	 * @param $id
	 * @return 
	 */
	public function del_group($id)
	{
		$result = array('error' => 0, 'content' => '删除成功');
		if($id)
		{
			$sql = "DELETE FROM ".$this->table('block')." WHERE  id='".$id."'";
			$res = $this->query($sql);	
			$sql = "DELETE FROM ".$this->table('block_content')." WHERE  block_id='".$id."'";
			$res2 = $this->query($sql);
			if(!$res || !$res2)
			{
				$result['error'] = 1;
				$result['content'] = '删除失败';
			}
		}
		return $result;
	}
	
	/**
	 * 删除版块详细内容
	 * @param $id
	 * @return
	 */
	public function del_img($id)
	{
		$result = array('error' => 0, 'content' => '删除成功');
		if($id)
		{
			$sql = "DELETE FROM ".$this->table('block_content')." WHERE  id='".$id."'";
			$res2 = $this->query($sql);
			if(!$res2)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
		}
		return $result;
	}
	
	/**
	 * 版块内容排序
	 * @param $val
	 * @param $id
	 * @return 
	 */
	public function ordering($val, $id)
	{
		$result = array('error' => 0, 'content' => '');
		$sql = "UPDATE ".$this->table('block_content')." SET ordering='$val' WHERE id='".$id."'";
		$res = $this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	
	/**
	 * 插入版块内容
	 * @param $info
	 * @return 
	 */
	public function add_images($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "INSERT INTO ".$this->table('block_content')." (".$data['key'].") VALUES(".$data['val'].")";
			$res=$this->query($sql);
			if($this->insert_id())
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 获得版块信息
	 * @param $page
	 * @return 
	 */
	public function ad_list($page, $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$result = $this->show_page("SELECT id,title,code,created,description FROM ".$this->table('block')." ORDER BY id DESC", $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		return $listPage;
	}
	
	/**
	 * 获取版块详情列表
	 * @param $flash_id
	 * @param $page
	 * @param $limit
	 * @return 
	 */
	public function content_list($flash_id, $page, $limit = 10)
	{
		$listPage=array('data' => '', 'pageLink' => '');
	   	if($flash_id)
	   	{
			$result = $this->show_page("SELECT id,title,ordering,created FROM ".$this->table('block_content')." WHERE block_id='".$flash_id."' ORDER BY id DESC",$page);
			if($result)
			{
				foreach($result['result'] as  $k => $v)
				{
					$result['result'][$k]['title'] = hy_dig_filter($v['title'], 1);
				}
			}
			$listPage['pagelink'] = $result['page'];
			$listPage['data'] = $result['result'];
	   	}
		return $listPage;
	}
	
	/**
	 * 获取版块详细的信息
	 * @param $id
	 * @return 
	 */
	public function getGroup($id)
	{
		$sql = "SELECT * FROM ".$this->table('block')." WHERE id = '".$id."'";
			
		$result = $this->getRow($sql);
		if($result)
		{
			//获取相册集
			$sql = "SELECT p.* FROM ".$this->table('block_content')." as p WHERE p.block_id = '".$id."'";
			$photo = $this->getAll($sql);
			foreach($photo as $k => $v)
			{
				$arr = hy_dig_filter($v['title'], 3);
				$photo[$k]['title'] = $arr[0];
			}
			$result['photo'] = $photo;
			return $result;
		}
		else
		{
			return $result = array();
		}
	}
	
	/**
	 * 检查版块位置是否存在
	 * @param $code
	 * @param $id
	 * @return boolean
	 */
	public function check_code($code, $id = 0)
	{
		if($id > 0)
		{
			$sql = "SELECT code FROM ".$this->table('block')." WHERE id='".$id."'";
			$oldcode = $this->getOne($sql);
			$code_exist = 0;
			if($oldcode != $code)
			{
				$sql = "SELECT count(*) FROM ".$this->table('block')." WHERE code='$code'";
				$code_exist = $this->getOne($sql);
			}
			
			if($code_exist>0)
			{	
				return false;
			}
			else 
			{
				return true;
			}
		}
		else 
		{
		    $sql="SELECT count(*) FROM ".$this->table('block')." WHERE code='$code'";
		    if($this->getOne($sql) > 0)
		   	{
				return false;
		   	}
		   	else 
		  	{
				return true;
		  	}
		}
	}
	
	/**
	 * 更新版块数据信息
	 * @param $info
	 * @param $id
	 */
	public function update_group($info, $id)
	{
		if($id)
		{
			$data = yf_set_update($info);
			$sql = "UPDATE ".$this->table('block')." SET ".$data." WHERE id='".$id."'";
			return $res = $this->query($sql);
		}
		else
		{
			return false;
		}
	}
}